<?php
namespace app\admin\model;

date_default_timezone_set('PRC'); 
use think\Model;
use think\Db;

class User extends Model {

    private $page = 1;
    private $limit = 10;

    public function getUserName($name) {
        return $list = Db::name('user')->where('name',$name)->find();
    }

    public function addBrand($name,$url) {
        return Db::name('brand')->data(['name'=>$name,'url'=>$url,'add_time'=>date('y-m-d h:i:s',time()),'update_time'=>date('y-m-d h:i:s',time())])->insert();
    }

    public function delBrand($id) {
        $params = array(
                "update_time" => date('y-m-d h:i:s',time()),
                "del_flag" => 1
            );
        return Db::name('brand')->where('id',$id)->update($params);
    }

    public function getBrandSearhList() {
        return Db::name('brand')->where('del_flag = 0')->select();
    }

    public function getBrandList($page) {
        $data = array();
        $limit = $this->limit;
        if (empty($page)) {
            $page = $this->page;
        } 
        $data['getPage'] = $page;
        $pageLimit = (($page - 1) * $limit ) > 0? (($page - 1) * $limit): 0;
        $data['list'] = Db::name('brand')->where('del_flag = 0')->order('id desc')->limit($pageLimit,$limit)->select();
        $data['count'] = count(Db::name('brand')->where('del_flag = 0')->select());
        
        $data['firstPage'] = 1;
        $data['lastPage'] = ceil($data['count']/$limit);

        if ($page < $data['firstPage']) {
            $page = $data['firstPage'];
        }
        if ($page > $data['lastPage']) {
            $page = $data['lastPage'];
        }

        $data['pageStart'] = 1;
        $data['pageEnd'] = $pageLimit + $limit;
        
        $data['thisPage'] = $page;

        
        if ($page == 1) {
            $data['pageList'] = array($page,$page+1,$page+2);
        } else if ($page == $data['lastPage']) {
            if ($data['lastPage']-2 > 0) {
                $data['pageList'] = array($data['lastPage']-2,$data['lastPage']-1,$data['lastPage']);
            } else if ($page - 1 > 0 && $data['lastPage']-2 == 0) {
                $data['pageList'] = array($data['lastPage']-1,$data['lastPage'],$data['lastPage']+1);
            } else {
                $data['pageList'] = array($page,$page+1,$page+2);
            }
        } else {
            if ($page-1 > 0) {
                $data['pageList'] = array($page-1,$page,$page+1);
            } else {
                $data['pageList'] = array($page,$page+1,$page+2);
            }
            
        }
        
        return $data;
    }

    public function getIntentionList($page) {
        $data = array();
        $limit = $this->limit;
        if (empty($page)) {
            $page = $this->page;
        } 
        $data['getPage'] = $page;
        $pageLimit = (($page - 1) * $limit ) > 0? (($page - 1) * $limit): 0;
        $data['list'] = Db::name('intention')->order('id desc')->limit($pageLimit,$limit)->select();
        $data['count'] = count(Db::name('intention')->select());
        
        $data['firstPage'] = 1;
        $data['lastPage'] = ceil($data['count']/$limit);

        if ($page < $data['firstPage']) {
            $page = $data['firstPage'];
        }
        if ($page > $data['lastPage']) {
            $page = $data['lastPage'];
        }

        $data['pageStart'] = 1;
        $data['pageEnd'] = $pageLimit + $limit;
        
        $data['thisPage'] = $page;

        
        if ($page == 1) {
            $data['pageList'] = array($page,$page+1,$page+2);
        } else if ($page == $data['lastPage']) {
            if ($data['lastPage']-2 > 0) {
                $data['pageList'] = array($data['lastPage']-2,$data['lastPage']-1,$data['lastPage']);
            } else if ($page - 1 > 0 && $data['lastPage']-2 == 0) {
                $data['pageList'] = array($data['lastPage']-1,$data['lastPage'],$data['lastPage']+1);
            } else {
                $data['pageList'] = array($page,$page+1,$page+2);
            }
        } else {
            if ($page-1 > 0) {
                $data['pageList'] = array($page-1,$page,$page+1);
            } else {
                $data['pageList'] = array($page,$page+1,$page+2);
            }
            
        }
        
        return $data;
    }

    public function addProduct($params) {
        $params['update_time'] = date('y-m-d h:i:s',time());
        return Db::name('product')->data($params)->insert();
    }

    public function updateProduct($id,$params) {
        return Db::name('product')->where('id', $id)->update($params);
    }


    public function getProductList($page,$serachBrandSelect) {
        $data = array();
        $limit = $this->limit;
        if (empty($page)) {
            $page = $this->page;
        } 
        $data['getPage'] = $page;
        $pageLimit = (($page - 1) * $limit ) > 0? (($page - 1) * $limit): 0;
        if ($serachBrandSelect != -1 && !empty($serachBrandSelect)) {
            $data['list'] = Db::name('product')
            ->where('del_flag = 0')
            ->where('brand_id', (int) $serachBrandSelect)
            ->order('id desc')->limit($pageLimit,$limit)->select();
        } else {
            $data['list'] = Db::name('product')->where('del_flag = 0')->order('id desc')->limit($pageLimit,$limit)->select();
        }
        $data['count'] = count(Db::name('product')->where('del_flag = 0')->select());
        
        $data['firstPage'] = 1;
        $data['lastPage'] = ceil($data['count']/$limit);

        if ($page < $data['firstPage']) {
            $page = $data['firstPage'];
        }
        if ($page > $data['lastPage']) {
            $page = $data['lastPage'];
        }

        $data['pageStart'] = 1;
        $data['pageEnd'] = $pageLimit + $limit;
        
        $data['thisPage'] = $page;
        $data['serachBrandId'] = $serachBrandSelect;
        
        if ($page == 1) {
            $data['pageList'] = array($page,$page+1,$page+2);
        } else if ($page == $data['lastPage']) {
            if ($data['lastPage']-2 > 0) {
                $data['pageList'] = array($data['lastPage']-2,$data['lastPage']-1,$data['lastPage']);
            } else if ($page - 1 > 0 && $data['lastPage']-2 == 0) {
                $data['pageList'] = array($data['lastPage']-1,$data['lastPage'],$data['lastPage']+1);
            } else {
                $data['pageList'] = array($page,$page+1,$page+2);
            }
        } else {
            if ($page-1 > 0) {
                $data['pageList'] = array($page-1,$page,$page+1);
            } else {
                $data['pageList'] = array($page,$page+1,$page+2);
            }
            
        }
        
        return $data;
    }

    public function getProduct($id) {
        return Db::name('product')->where('id',$id)->select();
    }


    public function delProduct($id) {
        $params = array(
                "update_time" => date('y-m-d h:i:s',time()),
                "del_flag" => 1
            );
        return Db::name('product')->where('id',$id)->update($params);
    }

    public function getBrandListIndex() {
        return Db::name('brand')->where('del_flag = 0')->limit(18)->column('id,name,url');
    }

    public function getProductListIndex() {
        return Db::name('product')->where('is_hot',1)->limit(4)->select();
    }

    public function delIntention($id) {
        return Db::name('intention')->where('id',$id)->delete();
    }
}